//
//  WriteCardTableViewController.m
//  ApexCardSDK_Demo_iOS
//
//  Created by 谭永明 on 16/9/24.
//  Copyright © 2016年 珠海艾派克科技股份有限公司. All rights reserved.
//

#import "WriteCardTableViewController.h"

@interface WriteCardTableViewController ()

@end

@implementation WriteCardTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
}

-(void)loadData {
    
    self.totalSize = 0;
    
    NSInteger count = 5;
    NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:count];
    for (int i = 1; i <= count; i++) {
        MCCardFile *cardFile = [[MCCardFile alloc] init];
        cardFile.name = [NSString stringWithFormat:@"img_%d.jpg",i];
        cardFile.filePath = [[NSBundle mainBundle] pathForResource:cardFile.name ofType:nil];
        NSMutableData *data = [NSMutableData dataWithContentsOfFile:cardFile.filePath];
        cardFile.data = data;
        cardFile.length = data.length;
        cardFile.createdTime = [[NSDate date] timeIntervalSince1970];
        
        [tempArr addObject:cardFile];
        
        self.totalSize += cardFile.length / 1024;
    }
    self.files = [tempArr copy];
}

-(NSString *)createCardName {
    return [NSString stringWithFormat:@"纪念卡_%d",arc4random_uniform(255)];
}

#pragma mark - public

-(void)writeCard {
    MCCard *card = [[MCCard alloc] init];
    card.name = [self createCardName];
    card.listFile = [self.files mutableCopy];
    
    MCCardFileWriter *writer = self.device.cardFileWriter;
    // 打包
    [writer pack:card];
    
    self.card = card;
    [self.tableView reloadData];
    
    // 写卡
    [writer write];
}

@end
